package com.teacher.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.teacher.pojo.entity.PartyActivists;
import com.teacher.pojo.entity.PartySchool;
import com.teacher.pojo.excel.PartyActivistsExcel;
import com.teacher.pojo.excel.PartySchoolExcel;
import com.teacher.service.IPartyActivistsService;
import com.teacher.service.IPartySchoolService;
import com.teacher.service.IPerStudentService;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zfm
 * @Date: 2021/7/30 16:17
 */
public class ExcelPartyActivistsListener extends AnalysisEventListener<PartyActivistsExcel> {
    private Long termId;
    private IPartyActivistsService iPartyActivistsService;

    private List<PartyActivists> partyActivistsList = new ArrayList<>();
    private IPerStudentService iPerStudentService;

    public ExcelPartyActivistsListener(Long termId, IPartyActivistsService iPartyActivistsService, IPerStudentService iPerStudentService) {
        this.termId = termId;
        this.iPartyActivistsService = iPartyActivistsService;
        this.iPerStudentService = iPerStudentService;
    }

    @Override
    public void invoke(PartyActivistsExcel partyActivistsExcel, AnalysisContext analysisContext) {
        if (iPerStudentService.isExit(partyActivistsExcel.getStudentKey())) {
            PartyActivists partyActivists = new PartyActivists();
            BeanUtils.copyProperties(partyActivistsExcel, partyActivists);
            partyActivists.setTermId(termId);
            partyActivistsList.add(partyActivists);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        iPartyActivistsService.saveBatch(partyActivistsList);
    }
}
